package com.bilibili.juc.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author zuife
 * @description
 * @since 2024-05-04 22:24:46
 */
public class LongAccumulatorDemo {
    public static final int _1W = 10000;
    public static final int threadNumber = 50;

    public static void main(String[] args) {
        ClickNumber clickNumber = new ClickNumber();
        //计算耗时
        long start;
        long end;

        CountDownLatch countDownLatch1 = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch2 = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch3 = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch4 = new CountDownLatch(threadNumber);
        start = System.currentTimeMillis();
        for (int i = 0; i < threadNumber; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < 100 * _1W; j++) {
                        clickNumber.clickBySynchronized();
                    }
                } finally {
                    countDownLatch1.countDown();
                }
            }, String.valueOf(i)).start();
        }
        try {
            countDownLatch1.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        end = System.currentTimeMillis();
        System.out.println("clickBySynchronized 耗时：" + (end - start) +"\t"+"结果："+clickNumber.num);

        start = System.currentTimeMillis();
        for (int i = 0; i < threadNumber; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < 100 * _1W; j++) {
                        clickNumber.clickByAtomicLong();
                    }
                } finally {
                    countDownLatch2.countDown();
                }
            }, String.valueOf(i)).start();
        }
        try {
            countDownLatch2.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        end = System.currentTimeMillis();
        System.out.println("clickByAtomicLong 耗时：" + (end - start) +"\t"+"结果："+clickNumber.atomicLong.get());

        start = System.currentTimeMillis();
        for (int i = 0; i < threadNumber; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < 100 * _1W; j++) {
                        clickNumber.clickByLongAdder();
                    }
                } finally {
                    countDownLatch3.countDown();
                }
            }, String.valueOf(i)).start();
        }
        try {
            countDownLatch3.await();
        }
        catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        end = System.currentTimeMillis();
        System.out.println("clickByLongAdder 耗时：" + (end - start) +"\t"+"结果："+clickNumber.longAdder.sum());


        start = System.currentTimeMillis();

        for (int i = 0; i < threadNumber; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < 100 * _1W; j++) {
                        clickNumber.clickByLongAccumulator();
                    }
                } finally {
                    countDownLatch4.countDown();
                }
            }, String.valueOf(i)).start();
        }
        try {
            countDownLatch4.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        end = System.currentTimeMillis();
        System.out.println("clickByLongAccumulator 耗时：" + (end - start) +"\t"+"结果："+clickNumber.longAccumulator.get());


    }
}

class ClickNumber {
    int num = 0;

    public synchronized void clickBySynchronized() {
        num++;
    }

    AtomicLong atomicLong = new AtomicLong(0);

    public void clickByAtomicLong() {
        atomicLong.getAndIncrement();
    }

    LongAdder longAdder = new LongAdder();

    public void clickByLongAdder() {
        longAdder.increment();
    }

    LongAccumulator longAccumulator = new LongAccumulator((left, right) -> left + right, 0);

    public void clickByLongAccumulator() {
        longAccumulator.accumulate(1);
    }

}
